package com.sokal.easycare.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.sys.entity.UserRealnameAuth;
import com.sokal.easycare.modules.sys.service.FileLibrayService;
import com.sokal.easycare.modules.sys.service.UserRealnameAuthService;

/**
 * 实名认证Controller
 * @author yangmin
 * @version 2017-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/userRealnameAuth")
public class UserRealnameAuthController extends BaseController {

	@Autowired
	private UserRealnameAuthService userRealnameAuthService;
	
	@Autowired
	FileLibrayService fileLibrayService ;
	
	@ModelAttribute
	public UserRealnameAuth get(@RequestParam(required=false) String id) {
		UserRealnameAuth entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userRealnameAuthService.get(id);
		}
		if (entity == null){
			entity = new UserRealnameAuth();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:userRealnameAuth:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserRealnameAuth userRealnameAuth, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserRealnameAuth> page = userRealnameAuthService.findPage(new Page<UserRealnameAuth>(request, response), userRealnameAuth); 
		model.addAttribute("page", page);
		return "modules/sys/userRealnameAuthList";
	}

	@RequiresPermissions("sys:userRealnameAuth:view")
	@RequestMapping(value = "form")
	public String form(UserRealnameAuth userRealnameAuth, Model model) {
		model.addAttribute("userRealnameAuth", userRealnameAuth);
		
		model.addAttribute("file1path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile1()).getPath());
		model.addAttribute("file2path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile2()).getPath());
		model.addAttribute("file3path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile3()).getPath());
		
		return "modules/sys/userRealnameAuthForm";
	}

	@RequiresPermissions("sys:userRealnameAuth:edit")
	@RequestMapping(value = "save")
	public String save(UserRealnameAuth userRealnameAuth, Model model, RedirectAttributes redirectAttributes) {
		userRealnameAuthService.save(userRealnameAuth);
		addMessage(redirectAttributes, "保存实名认证成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userRealnameAuth/?repage";
	}
	
	@RequiresPermissions("sys:userRealnameAuth:edit")
	@RequestMapping(value = "delete")
	public String delete(UserRealnameAuth userRealnameAuth, RedirectAttributes redirectAttributes) {
		userRealnameAuthService.delete(userRealnameAuth);
		addMessage(redirectAttributes, "删除实名认证成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userRealnameAuth/?repage";
	}


}